import { error, instantiate, Label, Prefab, resources, Vec3, Node, tween, randomRangeInt, random, v3, JsonAsset, math } from "cc";
import { Monster } from "./Monster";
import { Globals } from "./Globals";
import { Constant } from "./Constant";

export class Util {
    static showText(text: string, color: string, worldPos: Vec3, parent: Node) {


            const ndText = Globals.getNode(Constant.PrefabUrl.DAMAGE_TEXT, parent);
            ndText.getComponent(Label).string = text;
            // 设置位置得时候，要先将ndText.parent = parent; 否则找不到位置

            const newPos = v3(worldPos);
            newPos.add3f(randomRangeInt(-10, 10), 30, 0);

            ndText.setWorldPosition(newPos);
            ndText.getComponent(Label).color = math.color(color);

            // 放大->等待->消失

            ndText.setScale(1,1);

            tween(ndText)
            .to(0.2, {scale: new Vec3(1.5,1.5,ndText.scale.z)})
            .delay(0.5)
            .to(0.1, {scale: new Vec3(0.1,0.1,ndText.scale.z)})
            .call(()=>{
                ndText.destroy()
            })
            .start();
    }

    static createMonster(prefab: Prefab, parent: Node) {
                 const ndMonster = instantiate(prefab);
                 ndMonster.parent = parent;
                 return ndMonster;
    }


    static moveNode(node: Node, radian:number, distance:number) {
        const x = node.position.x + Math.cos(radian) * distance;
            const y = node.position.y + Math.sin(radian) * distance;
            node.setPosition(x,y,0);
    }

    static getPosition(start:Vec3, radian: number, distance: number, pos?:Vec3) {
        const x = start.x + Math.cos(radian) * distance;
        const y = start.y + Math.sin(radian) * distance;
        if(!pos) {
            pos = new Vec3();
        }
        return pos.set(x,y,start.z);
    }


    static getRadian(start:Vec3, end: Vec3) {
        return Math.atan2(end.y - start.y, end.x - start.x);
    }
}